package net.huashitong.Indicators.web;

import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.entity.TertiaryIndustry;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.service.IndicatorBreakdownService;
import net.huashitong.Indicators.service.IntelligentAnalysisReportService;
import net.huashitong.Indicators.service.TertiaryIndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 智能分析报告
 */
@Controller
@RequestMapping(value = "/app/aiReport")
public class IntelligentAnalysisReportController extends JsonBaseController {

    @Autowired
    private TertiaryIndustryService tertiaryIndustryService;

    @Autowired
    private IndicatorBreakdownService breakdownService;

    @Autowired
    private IntelligentAnalysisReportService service;

    @Override
    protected String getView(String s) {
        return null;
    }

    /**
     * @param filter
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dealReport", method = RequestMethod.POST)
    public Message dealReport(@RequestBody IndicatorBreakdownFilter filter) {
        //获取最新时间date、
        if (StringUtils.isBlank(filter.getDate())) {
            filter.setDate(breakdownService.getNewDate());
        }
        filter.setArea("永泰县");
        List<IndicatorBreakdown> allData = breakdownService.queryMonthValueSec(filter);
        //封装dataMap
        Map<String, IndicatorBreakdown> dataMap = new HashMap<>();
        List<String> indicatorList = new ArrayList<>();
        for (IndicatorBreakdown data : allData) {
            if (!dataMap.containsKey(data.getIndustryId())) {
                dataMap.put(data.getIndustryId(), data);
                indicatorList.add(data.getIndustryId());
            }
        }
        List<TertiaryIndustry> ts = tertiaryIndustryService.queryByIndicatorLevel("主要经济社会发展");
        Map<String, Object> resultMap = new HashMap<>();
        //达标情况
        Map<String, Object> reachStandardMap = service.reachStandardDetail(ts,dataMap);
        //GDP增长排名情况
        Map<String, Object> gdpRankMap =service.gdpRank(filter.getDate());
        //有效反馈企业问题领导
        Map<String, Object> effectiveInterViewMap =service.effectiveInterView(filter.getDate());
        //贡献比
        Map<String,Object> contributionMap = service.getContribution(filter.getDate());

        //排名末三
        Map<String,Object> rank3Map = service.getRank3(filter.getDate(),indicatorList);

        resultMap.put("date",filter.getDate());
        resultMap.put("noComplete", reachStandardMap.get("noComplete"));            //未达成目标
        resultMap.put("cityComplete", reachStandardMap.get("cityComplete"));        //达成市级目标
        resultMap.put("countyComplete", reachStandardMap.get("countyComplete"));    //达成县级目标
        resultMap.put("fightComplete", reachStandardMap.get("fightComplete"));      //达成争先目标
        resultMap.put("gdpRank", gdpRankMap.get("gdpRank"));                        //GDP排名
        resultMap.put("effectiveInterView", effectiveInterViewMap.get("effectiveInterView"));   //有效走访
        resultMap.put("contribution", contributionMap.get("contribution"));         //贡献度
        resultMap.put("rank3",rank3Map.get("rank3"));

        return coverMessage("200", "获取成功", resultMap);
    }

}
